package pl.enigmatic.parser.string;

import java.util.HashMap;

import pl.enigmatic.util.Java;


public final class EnumConverter<E> extends Converter<E> {

	private final HashMap<String, E> constants = new HashMap<String, E>();

	@SuppressWarnings("unchecked")
	public EnumConverter(final Class<E> clazz) {
		for (final Object e : clazz.getEnumConstants()) {
			constants.put(e.toString(), (E) e);
		}
	}

	@Override
	public E convert(final String value) {
		if (constants.containsKey(value)) {
			return constants.get(value);
		} else {
			return constants.get(Java.convertJavaStyleToConstantName(value));
		}
	}

	@Override
	public String convertBack(final E value) {
		return value.toString();
	}
}
